/**
 * Created by Administrator on 2017/3/6.
 */

var DBFactory = require('../conn/DBFactory');
var updateGoodsDao = module.exports;

updateGoodsDao.expired = function (data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        DBFactory.getConnection(function(error, connection) {
            if(error) {
                cb(error);
                return;
            } else {
                var sql = 'replace into expire_goods(`pictUrl`,`shopTitle`,`title`,`tkRate`,`tkCommFee`,`totalNum`,' +
                    '`couponEffectiveStartTime`,`couponEffectiveEndTime`,`biz30day`,`zkPrice`,`auctionUrl`,`couponAmount`,' +
                    '`hiddenActivityId`,`hiddenAmount`,`couponLeftCount`,`couponTotalCount`,`couponInfo`,`tag`,`auctionId`,' +
                    '`match_content`,`isHot`,`isCheap`,`isHighScore`,`isHighCoupon`,`isTMall`,`isRecommend`,`adTitle`,`uid_insert`,' +
                    '`sellerId`,`source`,`reservePrice`,`includeDxjh`,`auctionTag`,`userType`,`created_date`,`updated_date`,`eventCreatorId`,' +
                    '`eventRate`,`maxTkRate`,`eventEndTime`,`realTkRate`,`tkType`,`fqCat`,`planLink`,`isBrand`,`activityType`,`hiddenStartTime`,' +
                    '`hiddenEndTime`,`hiddenInfo`,`hiddenStartFee`,`hiddenLeftCount`,`hiddenTotalCount`,`nextRefreshTime`,`teamName`) select ' +
                    '`pictUrl`,`shopTitle`,`title`,`tkRate`,`tkCommFee`,`totalNum`,`couponEffectiveStartTime`,`couponEffectiveEndTime`,`biz30day`,' +
                    '`zkPrice`,`auctionUrl`,`couponAmount`,`hiddenActivityId`,`hiddenAmount`,`couponLeftCount`,`couponTotalCount`,`couponInfo`,`tag`,' +
                    '`auctionId`,`match_content`,`isHot`,`isCheap`,`isHighScore`,`isHighCoupon`,`isTMall`,`isRecommend`,`adTitle`,`uid_insert`,`sellerId`,' +
                    '`source`,`reservePrice`,`includeDxjh`,`auctionTag`,`userType`,`created_date`,`updated_date`,`eventCreatorId`,`eventRate`,`maxTkRate`,' +
                    '`eventEndTime`,`realTkRate`,`tkType`,`fqCat`,`planLink`,`isBrand`,`activityType`,`hiddenStartTime`,`hiddenEndTime`,`hiddenInfo`,' +
                    '`hiddenStartFee`,`hiddenLeftCount`,`hiddenTotalCount`,`nextRefreshTime`,`teamName` from goods where goods.id in (select id FROM goods where ' +
                    ' (unix_timestamp(now()) - unix_timestamp(`created_date`))/86400  >?  or unix_timestamp(now()) > unix_timestamp(`hiddenEndTime`) or (`zkPrice` - `couponAmount`) < 0  ' +
                    ' or (`zkPrice` - `hiddenAmount`) < 0 or NOW() > couponEffectiveEndTime) or hiddenActivityId is null or hiddenActivityId = ? ';
                connection.query(sql, [data.maxDay, ''], function(err, result){
                    if(err) {
                        cb(err);
                        console.log(err);
                        connection.release();
                        return;
                    } else {
                        var rows = {};
                        rows.backupsGoods = result.affectedRows;
                        var sql2 = 'DELETE FROM  goods where (unix_timestamp(now()) - unix_timestamp(`created_date`))/86400  > ? ' +
                            'or unix_timestamp(now()) > unix_timestamp(`hiddenEndTime`) or (`zkPrice` - `couponAmount`) < 0  ' +
                            'or (`zkPrice` - `hiddenAmount`) < 0 or NOW() > couponEffectiveEndTime or hiddenActivityId is null or hiddenActivityId = ? ';
                        connection.query(sql2, [data.maxDay, ''], function(err, result){
                            if(err) {
                                cb(err);
                                console.log(err);
                                connection.release();
                                return;
                            } else {
                                rows.expiredGoods = result.affectedRows;
                                cb(null, rows);
                                connection.release();
                                return;
                            }
                        });
                    }
                });
            }
        });
    }
}

updateGoodsDao.getAuctionIdsBySellerId = function (sellerId, limit, cb) {

    DBFactory.getConnection(function(error, connection) {
        if(error) {
            cb(error);
            return;
        } else {
            connection.query('select auctionId from goods where sellerId ' +
                (sellerId ? (' = ' + sellerId) : ' is null OR sellerId = 0 limit ') +
                (limit ? limit : 40), [], function(err, result) {

                if(err) {
                    cb(err);
                    connection.release();
                    return;
                } else {
                    cb(null, result);
                    connection.release();
                    return;
                }
            });
        }
    });
}

updateGoodsDao.runUpdateSql = function (sql, param, cb) {

    DBFactory.getConnection(function(error, connection) {
        if(error) {
            cb(error);
            return;
        } else {
            connection.query(sql, param, function(err, result) {
                cb(err, result);
                connection.release();
                return;
            });
        }
    });
}

updateGoodsDao.expiredPdGoods = function(data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        DBFactory.getConnection(function(error, connection) {
            if(error) {
                cb(error);
                return;
            } else {
                var sql = 'select pdId from goods where source = ? and pdId > 0 and ( ' +
                    ' (unix_timestamp(now()) - unix_timestamp(`created_date`))/86400  > ? ' +
                    ' or unix_timestamp(now()) > unix_timestamp(`hiddenEndTime`) ' +
                    ' or (`zkPrice` - `couponAmount`) < 0 ' +
                    ' or (`zkPrice` - `hiddenAmount`) < 0 ' +
                    ' or NOW() > couponEffectiveEndTime  ' +
                    ' or hiddenActivityId is null or hiddenActivityId = ? ) ';
                connection.query(sql, ['HTK', data.maxDay, ''], function(err, result){
                    if(err) {
                        cb(err);
                        console.log(err);
                        connection.release();
                        return;
                    } else {
                        var ids = [];
                        for(var i=0; i<result.length; i++) {
                            ids.push(result[i].pdId);
                        }
                        cb(null, ids);
                        connection.release();
                        return;
                    }
                });
            }
        });
    }
}